#'###########################################
#'                                          #
#'  ------------------------------------    #
#'  |     COIN     __                  |    #
#'  |            >(' )                 |    #
#'  |              )/                  |    #
#'  |             /  `----/            |    #
#'  |             \  ~=- /             |    #
#'  |   ~^~^~^~^~^~^~^~^~^~^~^~^~^~^   |    #
#'  ------------------------------------    #
#'                                          #
#'###########################################

rm(list=ls())
rm(.Random.seed)

#'#
#'# Chemins de travail
#'#
R_ROOT <- "C:/UQAR/Recherche/Maitrise/Donnees"
R_WORKING_DIRECTORY <- file.path(R_ROOT, "R")
R_DATA_PATH <- file.path(R_ROOT, "Donnees")
R_RESULTS_PATH <- file.path(R_ROOT, "Resultats")

R_STATIC_DIR <- file.path(R_DATA_PATH, "Bylot/Fixe")
R_MOBILE_DIR <- file.path(R_DATA_PATH, "Bylot/Mobile")

DATA_PATH <- file.path(R_RESULTS_PATH, "HRCompare")


#'#
#'# Chargement des dependances
#'# 

#source(file.path(R_WORKING_DIRECTORY, "Utils/initCluster.R"))
source(file.path(R_WORKING_DIRECTORY, "Utils/loadDependencies.R"))


#'#
#'# Chargement des donnees
#'# 

#' hrGraphsRadius
load(file.path(DATA_PATH, "hrGraphsRadius.RData"))
hrGraphsRadius[, method := as.factor(method)]

cols <- grey(c(0.8, 0.4))
cex.scales <- 0.9
cex.label <- 1.1

xlabels <- c("250", "500", "750", "1000", "2500", "5000")

#'#
#'# Comparaison de la taille
#'# 
hrComparedArea <- hrGraphsRadius[stat == "comparedArea"]
hrComparedArea[, mean := mean / 1000000]
hrComparedArea[, li := li / 1000000]
hrComparedArea[, ls := ls / 1000000]

gSize95 <- barchart(mean ~ type, data=hrComparedArea, groups=method, col=cols,
    scales =list(x=list(labels = xlabels), col=1, tck=c(1,0), cex=cex.scales, axs = "i"),
    origin = 0,
    ly=hrComparedArea$li,
    uy=hrComparedArea$ls,
    par.settings = list(axis.line = list(col="transparent"),
        layout.widths = list(left.padding=2, right.padding = 0)),
    prepanel=prepanel.errorbar, panel=panel.errorbar,
    ylab=list(expression(bold(paste("Home range size (", km^2, " \261 95% CI)", sep=""))), cex=cex.label, fontface="bold"),
    xlab=list("Radius (m)", cex=cex.label, fontface="bold"),
    addText=TRUE,
    textFUN=panel.text.title,
    text.letter="A"
)

hrSizeRatio <- hrGraphsRadius[stat == "sizeRatio"]
gSizeRatio95  <- barchart(mean ~ type, data=hrSizeRatio, groups=method, col=cols,
    scales=list(x=list(labels = xlabels), y=list(at=c(0, 1, 5, 10 ,15, 20, 25)), tck=c(1,0),
        cex=cex.scales, axs = "i", col=1),
    origin=0,
    ly=hrSizeRatio$li,
    uy=hrSizeRatio$ls,
    par.settings = list(axis.line = list(col="transparent"),
        layout.widths = list(left.padding=0, right.padding = 2)),
    prepanel=prepanel.errorbar, panel=panel.errorbar,
    key=list(text=list(c("Kernel",
                "MCP")), 
        rectangles=list(col=cols), x=0.9, y=0.90, corner=c(1, 1), cex=1, fontface="bold"),
    ylab=list("Size ratio Argos to GPS (\261 95% CI)", cex=cex.label, fontface="bold"),
    xlab=list("Radius (m)", cex=cex.label, fontface="bold"),
    addText=TRUE,
    textFUN=panel.text.title,
    text.letter="B"
)
gSizeRatio95 <- gSizeRatio95 + layer(panel.abline(h=1, lty=2))


hrPropInRef <- hrGraphsRadius[stat == "propInRef"]
gPropInRef95 <- barchart(mean ~ type, data=hrPropInRef, groups=method, col=cols,
    scales =list(x=list(labels = xlabels), col=1, tck=c(1,0), cex=cex.scales, axs = "i"),
    origin=0,
    ly=hrPropInRef$li,
    uy=hrPropInRef$ls,
    par.settings = list(axis.line = list(col="transparent"),
        layout.widths = list(left.padding=2, right.padding = 0)),
    prepanel=prepanel.errorbar, panel=panel.errorbar,
    ylab=list("Proportion of Argos locations in GPS HR (% \261 95% CI)", cex=cex.label, fontface="bold"),
    xlab=list("Radius (m)", cex=cex.label, fontface="bold"),
    addText=TRUE,
    textFUN=panel.text.title,
    text.letter="C"
)


print.multi(list(gSize95, gSizeRatio95, gPropInRef95), hfill=T, nrow=2, ncol=2,
    width=12, height=12, res=300, bitmap=TRUE, units="in", 
    save=TRUE, save.dest="C:/UQAR/Recherche/Maitrise/Donnees/Resultats/compareHRRadius10_95.tiff")
